<!-- 车辆列表 -->
<template>
  <div class="dashboard-container vehicle customer-list-box">
    <div class="app-container">
      <el-card
        class="search-card-box"
        shadow="never"
      >
        <el-form
          ref="form"
          :model="searchFormData"
          label-width="80px"
        >
          <div
            class="filter-container"
          >
            <el-row
              :gutter="60"
            >
              <el-col :span="8">
                <el-form-item
                  label="车辆类型:"
                  style="margin-bottom: 0px"
                >
                  <el-select
                    v-model="searchFormData.truckTypeId"
                    clearable
                    placeholder="请选择"
                  >
                    <el-option
                      v-for="item in vehicleTypelist"
                      :key="item.id"
                      :label="item.name"
                      :value="item.id"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item
                  label="车牌号码:"
                  style="margin-bottom: 0px"
                >
                  <el-input
                    v-model="searchFormData.licensePlate"
                    placeholder="请输入车牌号码"
                    clearable
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-button
                  type="warning"
                  @click="search"
                >搜索</el-button>
                <el-button
                  class="reset-btn"
                  plain
                  @click="reset"
                >重置</el-button>
              </el-col>
            </el-row>
          </div>
        </el-form>
      </el-card>
      <TabChange
        :default-active-index="2"
        :setting-list="settingList"
        @tabChange="tabChange"
      ></TabChange>
      <div
        v-loading="listLoading"
        :class="{ 'loading-box': listLoading }"
        element-loading-text="加载中"
        style="margin-top: 20px"
      >
        <el-card
          class="table-card-box"
          shadow="never"
        >
          <el-button
            type="warning"
            class="customer-add-btn"
            @click="addVehicle()"
          >新增车辆</el-button>
          <el-table
            :data="vehicleList"
            fit
            style="width: 100%"
            stripe
            :header-cell-style="{ background: 'rgba(250,252,255,1)' }"
          >
            <div
              v-show="
                (!vehicleList || vehicleList.length <= 0) &&
                  !listLoading &&
                  !searchkey
              "
              slot="empty"
            >
              <img
                src="@/icons/pic-kong.png"
                alt="img"
                style="margin-top: 20px; width: 25%; height: 25%"
              />
              <p style="margin-top: -20px; padding-bottom: 0px">这里空空如也</p>
            </div>
            <el-card
              v-show="
                (!vehicleList || vehicleList.length <= 0) && !listLoading && searchkey
              "
              slot="empty"
              class="table-empty-box"
              shadow="never"
            >
              <empty></empty>
            </el-card>
            <el-table-column
              align="left"
              label="序号"
              type="index"
              width="60"
            >
              <template v-slot="{ $index }">
                <span>{{ $index + (searchFormData.page - 1) * searchFormData.pageSize + 1 }}</span>
              </template>
            </el-table-column>
            <el-table-column
              align="left"
              label="车牌号码"
              min-width="102"
              prop="licensePlate"
            >
            </el-table-column>
            <el-table-column
              align="left"
              label="车辆类型"
              min-width="100"
              prop="truckTypeName"
            >
            </el-table-column>
            <el-table-column
              align="left"
              label="司机数量"
              min-width="100"
              prop="driverNum"
            >
            </el-table-column>
            <el-table-column
              align="left"
              label="车辆状态"
              min-width="100"
              prop="workStatus"
            >
              <template v-slot="{ row }">
                <div
                  class="tableColumn-status"
                  :class="{ 'stop-use': !row.workStatus }"
                >
                  {{ row.workStatus ? '可用' : '停用' }}
                </div>
              </template>
            </el-table-column>
            <el-table-column
              align="left"
              label="GPS设备id"
              min-width="150"
              prop="deviceGpsId"
            >
            </el-table-column>
            <el-table-column
              align="left"
              label="实载重量（T）"
              min-width="150"
              prop="allowableLoad"
            >
            </el-table-column>
            <el-table-column
              align="left"
              label="实载体积（方）"
              min-width="150"
              prop="allowableVolume"
            >
            </el-table-column>
            <el-table-column
              label="操作"
              align="center"
              fixed="right"
              min-width="220"
              class-name="small-padding fixed-width"
            >
              <template v-slot="{row}">
                <el-link
                  type="primary"
                  :underline="false"
                  @click="viewDetail(row.id)"
                >查看详情</el-link>
                <el-divider direction="vertical"></el-divider>
                <el-link
                  v-if="row.workStatus === 0"
                  type="primary"
                  :underline="false"
                  @click="startUse(row.id)"
                >启用</el-link>
                <el-link
                  v-else
                  type="danger"
                  :underline="false"
                  @click="stopUse(row.id)"
                >停用</el-link>
                <el-divider direction="vertical"></el-divider>
                <el-link
                  type="primary"
                  :underline="false"
                  @click="configuredriver(row)"
                >配置司机</el-link>
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页 -->
          <div class="pagination">
            <div class="pages">
              <el-pagination
                :current-page="searchFormData.page"
                :total="+total"
                :page-size="searchFormData.pageSize"
                :page-sizes="[10, 20, 30, 50]"
                layout="total, sizes, prev, pager, next, jumper"
                @size-change="sizeChange"
                @current-change="currentChange"
              />
            </div>
          </div>
        </el-card>
      </div>
    </div>
    <!-- 新增车辆弹窗 -->
    <VehicleAddDialog
      ref="vehicleAddDialog"
      class="add-form-dialog"
      :current-node-id="currentNodeId"
      @updateVehicleList="getList"
    />
    <!-- 配置司机弹窗 -->
    <ArrangeDriveDialog
      ref="arrangeDriveDialog"
      class="add-form-dialog"
      :title-info="titleInfo"
      :car-modal-info="carModalInfo"
      :unbind-drivers-list="unbindDriversList"
      @newDataes="getList"
    ></ArrangeDriveDialog>
  </div>
</template>
<script>
import { truckList, getCarTypeStatics, getCarTypeList, unBindDriverList, truckStatusUse, truckStatusNoUse } from '@/api/transit'
import TabChange from '@/components/TabChange'
import VehicleAddDialog from './components/vehicle-add.vue'
import ArrangeDriveDialog from './components/arrange-driver.vue'
export default {
  components: {
    TabChange,
    VehicleAddDialog,
    ArrangeDriveDialog
  },
  data() {
    return {
      listLoading: false,
      searchFormData: {
        page: 1,
        pageSize: 10,
        truckTypeId: '',
        licensePlate: '',
        workStatus: 2
      },
      total: 0,
      vehicleList: [],
      vehicleTypelist: [],
      searchkey: false,
      currentNodeId: null,
      settingList: [
        {
          value: 2,
          label: '全部',
          num: 0
        },
        {
          value: 1,
          label: '可用',
          num: 0
        },
        {
          value: 0,
          label: '停用',
          num: 0
        }
      ],
      titleInfo: {
        pageTitle: '',
        text: ''
      },
      carModalInfo: {},
      unbindDriversList: []
    }
  },
  created() {
    this.getList()
    this.getCarTypeStatics()
    this.getCarTypeList()
    const _this = this

    // 键盘enter操作
    document.onkeydown = function(e) {
      const key = window.event.keyCode
      if (key === 13) {
        _this.search()
      }
    }
  },
  methods: {
    async getList() {
      this.listLoading = true
      this.unBindDriverList()
      const params = { ...this.searchFormData }
      if (params.workStatus === 2) {
        delete params.workStatus
      }
      const { data } = await truckList(params)
      this.vehicleList = data.items
      this.total = data.counts
      this.listLoading = false
    },
    async getCarTypeStatics() {
      const { data } = await getCarTypeStatics()
      this.settingList[0].num = (+data[0] + +data[1]) || 0
      this.settingList[1].num = +data[1] || 0
      this.settingList[2].num = +data[0] || 0
    },
    async getCarTypeList() {
      const { data } = await getCarTypeList()
      this.vehicleTypelist = data
      this.$refs.vehicleAddDialog.getSettingList(data)
    },
    async unBindDriverList() {
      const { data } = await unBindDriverList()
      this.unbindDriversList = data
    },
    addVehicle() {
      this.$refs.vehicleAddDialog.openDialog()
    },
    sizeChange(val) {
      this.searchFormData.pageSize = val
      this.getList()
    },
    currentChange(val) {
      this.searchFormData.page = val
      this.getList()
    },
    search() {
      this.searchFormData.page = 1
      this.searchkey = true
      this.getList()
    },
    reset() {
      this.searchFormData.truckTypeId = ''
      this.searchFormData.licensePlate = ''
      this.searchkey = false
      this.getList()
    },
    tabChange(activeIndex) {
      this.searchFormData.workStatus = activeIndex
      this.searchFormData.page = 1
      this.getList()
    },
    viewDetail(id) {
      this.$router.push({
        path: '/transit/vehicle-detail',
        query: { id, formUrlName: '车辆管理' }
      })
    },
    startUse(id) {
      this.$confirm('<p style="margin-bottom: 9px; color: rgb(51, 41, 41);">确定要启用吗？车辆启用需满足以下条件：</p><span style="width: 16px; height: 16px; background: rgb(250, 252, 255); border: 1px solid rgb(235, 238, 245); border-radius: 50%; display: inline-block; text-align: center; margin-right: 6px; line-height: 14px;">1</span><span style="color: rgb(51, 41, 41);">车辆信息已完善</span><span style="width: 16px; height: 16px; background: rgb(250, 252, 255); border: 1px solid rgb(235, 238, 245); margin-left: 90px; border-radius: 50%; display: inline-block; text-align: center; line-height: 14px; margin-right: 6px;">2</span><span style="color: rgb(51, 41, 41);">绑定司机>=2,且有排班</span>',
        '车辆启用', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          customClass: 'startconfirm',
          dangerouslyUseHTMLString: true
        }).then(async() => {
        const res = await truckStatusUse(id)
        if (res.code === 200) {
          this.$message.success('启用成功')
          this.getList()
        } else {
          this.$message.error(res.msg)
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    },
    stopUse(id) {
      this.$confirm('<p style="margin-bottom: 9px; color: rgb(51, 41, 41);">确定要停用吗？车辆停用需满足以下条件：</p><span style="width: 16px; height: 16px; background: rgb(250, 252, 255); border: 1px solid rgb(235, 238, 245); border-radius: 50%; display: inline-block; text-align: center; margin-right: 6px; line-height: 14px;">1</span><span style="color: rgb(51, 41, 41);">车辆当前无运输任务</span><span style="width: 16px; height: 16px; background: rgb(250, 252, 255); border: 1px solid rgb(235, 238, 245); margin-left: 90px; border-radius: 50%; display: inline-block; text-align: center; line-height: 14px; margin-right: 6px;">2</span><span style="color: rgb(51, 41, 41);">车辆无关联运输线路</span><p style="color: rgb(255, 179, 2); margin-top: 10px; margin-bottom: -10px;">注：停用后司机将自动解除绑定</p>',
        '车辆停用', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          customClass: 'startconfirm',
          dangerouslyUseHTMLString: true
        }).then(async() => {
        const res = await truckStatusNoUse(id)
        if (res.code === 200) {
          this.$message.success('停用成功')
          this.getList()
        } else {
          this.$message.error(res.msg)
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    },
    configuredriver(row) {
      this.$confirm('<p style="margin-bottom: 9px; color: rgb(51, 41, 41);">配置司机需满足以下条件：</p><span style="width: 16px; height: 16px; background: rgb(250, 252, 255); border: 1px solid rgb(235, 238, 245); border-radius: 50%; display: inline-block; text-align: center; margin-right: 6px; line-height: 14px;">1</span><span style="color: rgb(51, 41, 41);">车辆信息已完善</span><span style="width: 16px; height: 16px; background: rgb(250, 252, 255); border: 1px solid rgb(235, 238, 245); margin-left: 90px; border-radius: 50%; display: inline-block; text-align: center; line-height: 14px; margin-right: 6px;">2</span><span style="color: rgb(51, 41, 41);">车辆无未完成运输任务</span>',
        '配置司机', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          customClass: 'startconfirm',
          dangerouslyUseHTMLString: true
        }).then(() => {
        this.titleInfo.text = '配置司机'
        Object.assign(this.carModalInfo, row)
        this.$nextTick(() => {
          this.$refs.arrangeDriveDialog.dialogFormV()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.alert {
  margin: 10px 0px;
}
.pagination {
  margin-top: 10px;
}
.vehicle ::v-deep .el-table td,
.el-table th {
  padding: 12px 0;
  min-width: 0;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  text-overflow: ellipsis;
  vertical-align: middle;
  position: relative;
  text-align: left;
  overflow: hidden;
}

.vehicle {
  ::v-deep .el-dialog__title {
    width: 73px;
    height: 25px;
    font-size: 18px;
    font-family: PingFangSC, PingFangSC-Regular;
    font-weight: 400;
    text-align: left;
    color: #20232a;
    line-height: 25px;
    letter-spacing: 0px;
  }
  ::v-deep .el-dialog__body {
    text-align: center;
    padding: 20px 20px 0px 20px;
  }
  .warn-img {
    width: 40px;
  }
  p {
    height: 24px;
    font-size: 16px;
    font-family: PingFangSC, PingFangSC-Regular;
    font-weight: 400;
    color: #818693;
    line-height: 24px;
  }
}

// 下拉框
::v-deep .el-select {
  width: 100%;
}
</style>

<style>
  .startconfirm{
    width: 481px;

    }
    .startconfirm .el-message-box__header{
      padding-bottom: 4px;
    }
    .startconfirm .el-message-box__container{
      margin-bottom: 15px;
    }
    .confirm .el-message-box__status.el-icon-warning{
      left: 70px;
    }
    .confirm.el-message-box__title{
      font-size: 16px;
    }
    .confirm .el-message-box__message p {
      color: #F92D2D;
      margin-top: 5px;
    }
    .confirm .el-message-box__btns{
      text-align: center;
      margin-top: 10px;
    }
</style>
